<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="./js/vue.js"></script>
  <style type="text/css">
    .grid {
      margin: auto;
      width: 500px;
      text-align: center;
    }

    .grid table {
      width: 100%;
      border-collapse: collapse;
    }

    .grid th,
    td {
      padding: 10;
      border: 1px dashed orange;
      height: 35px;
      line-height: 35px;
    }

    .grid th {
      background-color: orange;
    }
  </style>
</head>

<body>

  <div id="app">
    <div class="grid">
      <div>
        <h1>图书管理</h1>
        <div class="book">
          <div>
            <label for="id">
              编号:
            </label>
            <input type="text" id="id" v-model="id" :disabled="disabled">
            <label for="name">
              名称:
            </label>
            <input type="text" id="name" v-model="name">
            <button @click="handle">提交</button>
          </div>
        </div>
      </div>
      <table>
        <thead>
          <tr>
            <th>编号</th>
            <th>名称</th>
            <th>时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr :key='item.id' v-for='item in books'>
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.date}}</td>
            <td>
              <a href="" @click.prevent='toEdit(item.id)'>修改</a>
              <span>|</span>
              <a href="" @click.prevent='del(item.id)'>删除</a>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>

  <script lang="text/javascript">
    /*
  图书管理-图书列表展示功能
  注意事项：<a href="" @click.prevent>修改</a>
  事件绑定时，可以只添加修饰符，而不绑定事件函数
*/
    var vm = new Vue({
      el: '#app',
      data: {
        id: '',
        name: '',
        disabled: false,
        books: [{
          id: 1,
          name: '三国演义',
          date: ''
        }, {
          id: 2,
          name: '水浒传',
          date: ''
        }, {
          id: 3,
          name: '红楼梦',
          date: ''
        }, {
          id: 4,
          name: '西游记',
          date: ''
        }]
      },
      methods: {
        handle: function () {
          // 编辑操作
          // 就是根据当前的id去更新数组中的对应数据
          if (this.disabled) {
            this.books.forEach(item => {ß
              if (item.id == this.id) {
                item.name = this.name
                //完成更新操作,之后需要终止循环
                return;
              }
            });
            this.disabled = false;
          } else {
            // 添加图书
            let book = {};
            book.id = this.id;
            book.name = this.name;
            this.books.push(book);
          }

          this.id = ''
          this.name = ''
        },
        toEdit(id) {
          // 根据id 把 book 过滤出来
          let book = this.books.filter(function (each) {
            return each.id == id;
          });

          this.id = book[0].id;
          this.name = book[0].name;

          // 将id的input框disabled掉
          this.disabled = true;
        },
        del:function(id){
          // 删除图书
          // 根据id从数组中查找元素的所有
          // let index = this.books.findIndex(function(item){
          //   return item.id == id;
          // });
          // // 根据索引删除数组元素
          // this.books.splice(index,1);
          
          // 方法2 : 通过filter进行删除,就是把不符合条件的过滤掉
          this.books = this.books.filter(function(item){
            return item.id != id;
          });
        }

      }
    });



  </script>

</body>

</html>